package me.zhouzhuo810.magpiex.utils;


import android.os.Build;
import android.text.TextUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import androidx.annotation.ChecksSdkIntAtLeast;
import androidx.annotation.RequiresApi;

/**
 * 系统工具类，判断当前手机系统
 *
 * @author zhouzhuo810
 * @date 6/15/21 2:23 PM
 */
public class OSUtils {
    
    public static final String ROM_MIUI = "MIUI";
    public static final String ROM_EMUI = "EMUI";
    public static final String ROM_FLYME = "FLYME";
    public static final String ROM_OPPO = "OPPO";
    public static final String ROM_SMARTISAN = "SMARTISAN";
    public static final String ROM_VIVO = "VIVO";
    public static final String ROM_QIKU = "QIKU";
    
    private static final String KEY_VERSION_MIUI = "ro.miui.ui.version.name";
    private static final String KEY_VERSION_EMUI = "ro.build.version.emui";
    private static final String KEY_VERSION_OPPO = "ro.build.version.opporom";
    private static final String KEY_VERSION_SMARTISAN = "ro.smartisan.version";
    private static final String KEY_VERSION_VIVO = "ro.vivo.os.version";
    
    private static String sName;
    private static String sVersion;
    
    public static boolean isEmui() {
        return check(ROM_EMUI);
    }
    
    public static boolean isMiui() {
        return check(ROM_MIUI);
    }
    
    public static boolean isVivo() {
        return check(ROM_VIVO);
    }
    
    public static boolean isOppo() {
        return check(ROM_OPPO);
    }
    
    public static boolean isFlyme() {
        return check(ROM_FLYME);
    }
    
    public static boolean is360() {
        return check(ROM_QIKU) || check("360");
    }
    
    public static boolean isSmartisan() {
        return check(ROM_SMARTISAN);
    }
    
    public static String getName() {
        if (sName == null) {
            check("");
        }
        return sName;
    }
    
    public static String getVersion() {
        if (sVersion == null) {
            check("");
        }
        return sVersion;
    }
    
    public static boolean check(String rom) {
        if (sName != null) {
            return sName.equals(rom);
        }
        
        if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_MIUI))) {
            sName = ROM_MIUI;
        } else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_EMUI))) {
            sName = ROM_EMUI;
        } else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_OPPO))) {
            sName = ROM_OPPO;
        } else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_VIVO))) {
            sName = ROM_VIVO;
        } else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_SMARTISAN))) {
            sName = ROM_SMARTISAN;
        } else {
            sVersion = Build.DISPLAY;
            if (sVersion.toUpperCase().contains(ROM_FLYME)) {
                sName = ROM_FLYME;
            } else {
                sVersion = Build.UNKNOWN;
                sName = Build.MANUFACTURER.toUpperCase();
            }
        }
        return sName.equals(rom);
    }
    
    public static String getProp(String name) {
        String line = null;
        BufferedReader input = null;
        try {
            Process p = Runtime.getRuntime().exec("getprop " + name);
            input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
            line = input.readLine();
            input.close();
        } catch (IOException ex) {
            return null;
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return line;
    }
    
    @ChecksSdkIntAtLeast(api = Build.VERSION_CODES.LOLLIPOP)
    public static boolean isAndroid5() {
        return Build.VERSION.SDK_INT >= 21;
    }
    
    @ChecksSdkIntAtLeast(api = Build.VERSION_CODES.M)
    public static boolean isAndroid6() {
        return Build.VERSION.SDK_INT >= 23;
    }
    
    @ChecksSdkIntAtLeast(api = Build.VERSION_CODES.N)
    public static boolean isAndroid7() {
        return Build.VERSION.SDK_INT >= 24;
    }
    
    @ChecksSdkIntAtLeast(api = Build.VERSION_CODES.O)
    public static boolean isAndroid8() {
        return Build.VERSION.SDK_INT >= 26;
    }
    
    @ChecksSdkIntAtLeast(api = Build.VERSION_CODES.P)
    public static boolean isAndroid9() {
        return Build.VERSION.SDK_INT >= 28;
    }
    
    @ChecksSdkIntAtLeast(api = Build.VERSION_CODES.Q)
    public static boolean isAndroid10() {
        return Build.VERSION.SDK_INT >= 29;
    }
    
    @ChecksSdkIntAtLeast(api = Build.VERSION_CODES.R)
    public static boolean isAndroid11() {
        return Build.VERSION.SDK_INT >= 30;
    }
    
    @ChecksSdkIntAtLeast(api = Build.VERSION_CODES.S)
    public static boolean isAndroid12() {
        return Build.VERSION.SDK_INT >= 31;
    }
    
    /**
     * 判断是不是OPPO折叠屏设备
     */
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public static boolean isOPPOFoldableDevice() {
        boolean isFold = false;
        try {
            Class<?> cls = Class.forName("com.oplus.content.OplusFeatureConfigManager");
            Method instance = cls.getMethod("getInstance");
            Object configManager = instance.invoke(null);
            Method hasFeature = cls.getDeclaredMethod("hasFeature", String.class);
            Object object = hasFeature.invoke(configManager, "oplus.hardware.type.fold");
            if (object instanceof Boolean) {
                isFold = (boolean) object;
            }
        } catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return isFold;
    }
    
    /**
     * 判断是不是小米折叠屏设备
     */
    public static boolean isXiaoMiFoldableDevice() {
        return SystemProperties.getInt("persist.sys.muiltdisplay_type", 0) == 2;
    }
    
    /**
     * 判断是不是VIVO折叠屏设备
     */
    private static boolean isVIVOFoldableDevice(){
        try {
            Class<?>  c= Class.forName("android.util.FtDeviceInfo");
            Method  m = c.getMethod("getDeviceType");
            Object dType = m.invoke(c);
            return "foldable".equals(dType);
        } catch (Exception e) {
            e.printStackTrace();
            
        }
        return false;
    }
}


